package com.fagertveit.android.game.cb;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.fagertveit.android.game.framework.Game;
import com.fagertveit.android.game.framework.Input.TouchEvent;
import com.fagertveit.android.game.framework.gl.Camera2D;
import com.fagertveit.android.game.framework.gl.SpriteBatcher;
import com.fagertveit.android.game.framework.impl.GLScreen;
import com.fagertveit.android.game.framework.geom.Collision;
import com.fagertveit.android.game.framework.geom.Rectangle;
import com.fagertveit.android.game.framework.geom.Vector2;

public class MenuScreen extends GLScreen {
	Camera2D guiCam;
	SpriteBatcher batcher;
	Rectangle menuStart;
	Rectangle menuSettings;
	Rectangle menuQuit;
	Vector2 touchPoint;
	boolean[] activeBtn;
	
	public MenuScreen(Game game) {
		super(game);
		guiCam = new Camera2D(glGraphics, 640, 480);
		batcher = new SpriteBatcher(glGraphics, 500);
		menuStart = new Rectangle(256, 256, 128, 42);
		menuSettings = new Rectangle(256, 320, 128, 42);
		menuQuit = new Rectangle(256, 384, 128, 42);
		touchPoint = new Vector2();
		activeBtn = new boolean[3];
		activeBtn[0] = false;
		activeBtn[1] = false;
		activeBtn[2] = false;
	}
	@Override
	public void update(float deltaTime) {
		// TODO Auto-generated method stub
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();
		int len = touchEvents.size();
		for(int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if(event.type == TouchEvent.TOUCH_UP) {
				touchPoint.set(event.x, event.y);
				touchPoint = guiCam.touchToWorld(touchPoint);
				if(Collision.collide(menuStart, touchPoint)) {
					activeBtn[0] = true;
					game.setScreen(new GameScreen(game));
				} else {
					activeBtn[0] = false;
				}
				if(Collision.collide(menuSettings, touchPoint)) {
					activeBtn[1] = true;
				} else {
					activeBtn[1] = false;
				}
				if(Collision.collide(menuQuit, touchPoint)) {
					activeBtn[2] = true;
				} else {
					activeBtn[2] = false;
				}
			}
		}
	}

	@Override
	public void render(float deltaTime) {
		// TODO Auto-generated method stub
		GL10 gl = glGraphics.getGL();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		guiCam.setViewportAndMatrices();
		
		gl.glEnable(GL10.GL_TEXTURE_2D);
		
		batcher.beginBatch(Assets.menu);
		batcher.drawSprite(0, 0, 640, 480, Assets.menuBackRegion);
		batcher.drawSprite(256, 256, 128, 48, activeBtn[0]?Assets.menuBtnOn:Assets.menuBtnOff);
		batcher.drawSprite(256, 320, 128, 48, activeBtn[1]?Assets.menuBtnOn:Assets.menuBtnOff);
		batcher.drawSprite(256, 384, 128, 48, activeBtn[2]?Assets.menuBtnOn:Assets.menuBtnOff);
		batcher.endBatch();

		Assets.brickX2.Render(glGraphics, 100, 100);
		
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		batcher.beginBatch(Assets.menu);
		
		batcher.drawSprite(506, 32, 128, 128, Assets.soundIcon);
		
		batcher.endBatch();
		
		gl.glDisable(GL10.GL_BLEND);
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

}
